namespace Yaaf.WP7.NativeAccess.IO
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Linq;

    using Yaaf.WP7.NativeAccess.ComWrapper.HTC.COM_RegRw;

    public class ExtensionInformation
    {
        private static Dictionary<string, int> dict;

        const string AmazonKindleId = "48195FB4-EE0E-E011-9264-00237DE2DB9E";


        const string AmazonKindlePath = @"\Applications\Data\"+AmazonKindleId+@"\Data\IsolatedStore\kindle\";

        public static bool TryGetValue(string extension, out int key)
        {
            return PrivateDictionary.TryGetValue(extension, out key);
        }

        private const string AdobeReaderId = "BC4F319A-9A9A-DF11-A490-00237DE2DB9E";
        const string AdobeReaderDataPath = @"\Applications\Data\"+AdobeReaderId+@"\Data\IsolatedStore\";


        public static void OpenFile(FileInfo file)
        {
            string key = file.Extension.ToUpper();
            if (string.IsNullOrEmpty(key))
            {
                throw new InvalidOperationException("No extension to execute!");
            }

            key = key.Substring(1);
            int num;
            if (ExtensionInformation.TryGetValue(key, out num))
            {
                HTCRegistry reg;
                switch (num)
                {
                    case 0x20:
                        // Copy PDF file!
                        using (reg = new HTCRegistry())
                        {
                            if (!reg.IsAPExist(AdobeReaderId))
                            {
                                throw new NotSupportedException("Install the Adobe Reader to open Pdf files");
                            }
                        }
                        
                        if (!file.FullName.StartsWith(AdobeReaderDataPath))
                        {
                            var dir = new DirectoryInfo(AdobeReaderDataPath);
                            if (!dir.Exists)
                            {
                                dir.Create();
                            }
                            file = file.CopyTo(dir, overwrite: true) as FileInfo;
                        }
                        break;
                    case 0x3a:
                        //// Amazon kindle book .mobi
                        using (reg = new HTCRegistry())
                        {
                            if (!reg.IsAPExist(AmazonKindleId))
                            {
                                throw new NotSupportedException("Install Amazon Kindle to open .mobi files");
                            }
                        }

                        if (!file.FullName.StartsWith(AmazonKindlePath))
                        {
                            var dir = new DirectoryInfo(AmazonKindlePath);
                            if (!dir.Exists)
                            {
                                dir.Create();
                            }
                            file = file.CopyTo(dir, overwrite: true) as FileInfo;
                        }

                        using (reg = new HTCRegistry())
                        {
                            if (!reg.LaunchAP(AmazonKindleId))
                            {
                                throw new InvalidOperationException(
                                    "Could not launch Amazon Kindle (start it yourself, you will see the book)!");
                            }
                        }
                        return;
                    default:
                        break;
                }
            }

            file.Execute();
        }

        private static Dictionary<string, int> PrivateDictionary
        {
            get
            {
                if (dict == null)
                {
                    Dictionary<string, int> dictionary1 = new Dictionary<string, int>(0x3b);
                    dictionary1.Add("DOC", 0x0);
                    dictionary1.Add("DOCX", 0x1);
                    dictionary1.Add("DOCM", 0x2);
                    dictionary1.Add("DOT", 0x3);
                    dictionary1.Add("DOTX", 0x4);
                    dictionary1.Add("DOTM", 0x5);
                    dictionary1.Add("XLS", 0x6);
                    dictionary1.Add("XLSX", 0x7);
                    dictionary1.Add("XLSM", 0x8);
                    dictionary1.Add("XLT", 0x9);
                    dictionary1.Add("XLTX", 0xa);
                    dictionary1.Add("XLTM", 0xb);
                    dictionary1.Add("PPT", 0xc);
                    dictionary1.Add("PPTX", 0xd);
                    dictionary1.Add("PPTM", 0xe);
                    dictionary1.Add("PPS", 0xf);
                    dictionary1.Add("PPSX", 0x10);
                    dictionary1.Add("PPSM", 0x11);
                    dictionary1.Add("TXT", 0x12);
                    dictionary1.Add("RTF", 0x13);
                    dictionary1.Add("CSS", 0x14);
                    dictionary1.Add("JS", 0x15);
                    dictionary1.Add("RGU", 0x16);
                    dictionary1.Add("HTM", 0x17);
                    dictionary1.Add("HTML", 0x18);
                    dictionary1.Add("XHT", 0x19);
                    dictionary1.Add("XHTML", 0x1a);
                    dictionary1.Add("URL", 0x1b);
                    dictionary1.Add("XML", 0x1c);
                    dictionary1.Add("PROVXML", 0x1d);
                    dictionary1.Add("XSL", 0x1e);
                    dictionary1.Add("XAML", 0x1f);
                    dictionary1.Add("PDF", 0x20);
                    dictionary1.Add("BMP", 0x21);
                    dictionary1.Add("JPG", 0x22);
                    dictionary1.Add("JPEG", 0x23);
                    dictionary1.Add("GIF", 0x24);
                    dictionary1.Add("PNG", 0x25);
                    dictionary1.Add("ICO", 0x26);
                    dictionary1.Add("TIF", 0x27);
                    dictionary1.Add("TIFF", 0x28);
                    dictionary1.Add("WAV", 0x29);
                    dictionary1.Add("WMA", 0x2a);
                    dictionary1.Add("MP3", 0x2b);
                    dictionary1.Add("AAC", 0x2c);
                    dictionary1.Add("M4A", 0x2d);
                    dictionary1.Add("AVI", 0x2e);
                    dictionary1.Add("WMV", 0x2f);
                    dictionary1.Add("MP4", 0x30);
                    dictionary1.Add("MPG", 0x31);
                    dictionary1.Add("MPEG", 0x32);
                    dictionary1.Add("M4V", 0x33);
                    dictionary1.Add("ZIP", 0x34);
                    dictionary1.Add("EXE", 0x35);
                    dictionary1.Add("DLL", 0x36);
                    dictionary1.Add("CPL", 0x37);
                    dictionary1.Add("CER", 0x38);
                    dictionary1.Add("PFX", 0x39);
                    dictionary1.Add("MOBI", 0x3a);
                    dictionary1.Add("", 0x3b);
                    dict = dictionary1;
                }

                return dict;
            }
        }

        public static Dictionary<string, int> Dictionary
        {
            get
            {
                return new Dictionary<string, int>(PrivateDictionary);
            }
        }
    }
}